define(function(require){
    var main = require('/static/main.js');
    var m = new main();
    var menu = {
        isActive : false,
        menuindex :-1,
        setindex:0,
        settingaction : false,
        menuList : [],
        actionList : [],
        actionIndex:-1,
        ControllerId:0,
        table : '',
        init:function(){
            this.bindEvent();
            this.getList();
        },
        getList:function(){
            //获取管理员列表
            menu.table = $("#datatable").dataTable({
                "ordering" : false,
                "serverSide": true,
                "bSort": false,
                ajax: function (data, callback, settings){
                    //封装请求参数
                    var param = {};
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/jurisdiction/controller-info-get-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result){
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            menu.menuList = result.data;
                            callback(returnData);
                        }
                    });
                },
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": false,
                "autoWidth": false,
                "aoColumns": [
                    {"mDataProp": "ControllerName"},
                    {"mDataProp": "ControllerCode"},
                    {"mDataProp": "ControllerId",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<div class="option_btn"><span class="EditMenu" data-toggle="modal" data-target="#AddMenu">编辑</span>\
                               <span class="btn-setting">设置</span>');

                        }
                    },
                ],
                "fnCreatedRow": function (nRow, aData, iDataIndex) {
                },
                "fnInitComplete": function (oSettings, json) {
                }
            });
        },
        getActionList:function(){
                /******************************************************************************************************************************************/
            //获取模块信息列表
            menu.action = $("#actiontable").dataTable({
                //"ajax": {
                //    "url":"/jurisdiction/act-info-get-list",
                //    "type":'post',
                //    "data": function (d) {
                //        //添加额外的参数传给服务器
                //        return  {ControllerId: menu.ControllerId};
                //    }},
                ajax: function (data, callback, settings){

                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/jurisdiction/act-info-get-list",
                        data: {ControllerId:menu.ControllerId},  //传入组装的参数
                        dataType: "json",
                        success: function (result){
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.total = result.total;
                            returnData.data = result.data;
                            menu.actionList = result.data;
                            callback(returnData);
                        }
                    });
                },
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                "searching":false,
                'bLengthChange': false,

                "aoColumns": [
                    {"mDataProp": "ActionId",
                        'fnCreatedCell':function(nTd, sData, oData, iRow, iCol){
                            menu.settingaction = false;
                            $(nTd).html('<label class="cr-styled"><input '+ (oData.IsCheck ? 'checked="" ' : '')+' type="checkbox" class="todo-done" data-actionid="'+ oData.ActionId+'" ><i class="fa"></i></label>');
                        }
                    },
                    {"mDataProp": "ActionName"},
                    {"mDataProp": "ActionCode"},
                    {"mDataProp": "Sort",
                    'fnCreatedCell':function(nTd, sData, oData, iRow, iCol){
                        $(nTd).html('<input type="text" id="" class="form-control table-sort-input" value="'+ oData.Sort+'" style="" >');
                    }
                    },
                    {"mDataProp": "ActionId",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<div class="option_btn"><span class="EditAction" data-toggle="modal" data-target="#AddAction">编辑</span>');
                        }
                    },
                ]
            });
        },
        bindEvent:function(){
            $(document).on('click','.addMenu',function(){
                $("#addMenu .modal-title").html("添加菜单");
                menu.showmessage($("#addMenu .error"),false);
                menu.resetFrom($("#addMenu"));
                menu.menuindex = -1;
            });
            //添加菜单
            var createaction = false;
            $(document).on('click','#addMenu .btn-sure',function(){
                if(createaction){
                    return false;
                }
                var JsonData ={
                    ControllerName : $("#addMenu .ControllerName").val().trim(),
                    ControllerCode : $("#addMenu .ControllerCode").val().trim(),
                    LeftIcon : $("#addMenu .LeftIcon").val().trim(),
                    Sort : $("#addMenu .Sort").val().trim(),
                    AppRange : $("#addMenu .AppRange").val().trim(),
                }
                if(JsonData.ControllerName.length < 1){
                    menu.showmessage($("#addMenu .error"),true,'请输入菜单名称');
                    return false;
                }
                if(JsonData.ControllerCode.length < 1){
                    menu.showmessage($("#addMenu .error"),true,'请输入菜单编码');
                    return false;
                }
                if(JsonData.LeftIcon.length < 1){
                    menu.showmessage($("#addMenu .error"),true,'请输入样式编码');
                    return false;
                }
                if(JsonData.AppRange.length < 1){
                    menu.showmessage($("#addMenu .error"),true,'请选择适用范围');
                    return false;
                }
                menu.showmessage($("#addMenu .error"),false);
                menu.resetFrom($("#addMenu"));
                JsonData.Sort = JsonData.Sort.length > 0 ?  JsonData.Sort : 0;
                if(menu.menuindex >= 0){
                    JsonData.ControllerId = menu.menuList[menu.menuindex].ControllerId;
                }
                createaction = true;
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: '/jurisdiction/controller-info-create',
                    data:JsonData,
                    success: function (ret) {
                        createaction = false;
                        $("#addMenu").modal('hide');
                        menu.resetFrom($("#addMenu"));
                        if(ret.IsBizSuccess){
                            var table = $('#datatable').DataTable();
                            table.ajax.reload();
                            if(menu.menuindex >= 0){
                                layer.msg('修改成功！');
                            }else{
                                layer.msg('添加成功！');
                            }
                        }else{
                            if(menu.menuindex >= 0){
                                layer.msg('修改失败！');
                            }else{
                                layer.msg('添加失败！');
                            }
                        }
                    }
                });
            });
            //编辑菜单
            $(document).on('click','.EditMenu',function(){
                $("#addMenu .modal-title").html("编辑菜单");
                var index = $(this).closest("tr").index();
                menu.menuindex = index;
                var data = menu.menuList[index];
                $("#addMenu .ControllerName").val(data.ControllerName);
                $("#addMenu .ControllerCode").val(data.ControllerCode);
                $("#addMenu .LeftIcon").val(data.LeftIcon);
                $("#addMenu .Sort").val(data.Sort);
                $("#addMenu .AppRange").val(data.AppRange);
                menu.showmessage($("#addMenu .error"),false);
                $("#addMenu").modal("show");
            });

            //设置
            $(document).on('click','#datatable .btn-setting',function(){
                $("#actiontable").show();
                $(".map-btn").show();
                if(menu.settingaction){
                    return false;
                }
                var index = $(this).closest("tr").index();
                menu.setindex = index;
                var data = menu.menuList[index];
                menu.ControllerId  = data.ControllerId;
                menu.settingaction = true;
                if ($("#actiontable").hasClass("dataTable")) {
                    var dttable = $("#actiontable").dataTable();
                    dttable.fnClearTable(); //清空一下table
                    dttable.fnDestroy(); //还原初始化了的datatable
                }
                menu.getActionList();

            });

            //添加模块
            $(document).on('click','.addAction',function(){
                $("#addAction .modal-title").html("添加模块");
                menu.showmessage($("#addAction .error"),false);
                menu.resetFrom($("#addAction"));
                menu.actionindex = -1;
            });
            //添加模块
            var actionaction = false;
            $(document).on('click','#addAction .btn-sure',function(){
                if(actionaction){
                    return false;
                }
                var JsonData ={
                    ActionName : $("#addAction .ActionName").val().trim(),
                    ActionCode : $("#addAction .ActionCode").val().trim(),
                    AppRange : $("#addAction .AppRange").val().trim()
                }
                if(JsonData.ActionName.length < 1){
                    menu.showmessage($("#addAction .error"),true,'请输入菜单名称');
                    return false;
                }
                if(JsonData.ActionCode.length < 1){
                    menu.showmessage($("#addAction .error"),true,'请输入菜单编码');
                    return false;
                }
                if(JsonData.AppRange.length < 1){
                    menu.showmessage($("#addAction .error"),true,'请选择适用范围');
                    return false;
                }
                menu.showmessage($("#addAction .error"),false);
                menu.resetFrom($("#addAction"));
                if(menu.actionindex >= 0){
                    JsonData.ActionId = menu.actionList[menu.actionindex].ActionId;
                }
                createaction = true;
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: '/jurisdiction/action-info-create',
                    data:JsonData,
                    success: function (ret) {
                        createaction = false;
                        $("#addAction").modal('hide');
                        menu.resetFrom($("#addAction"));
                        if(ret.IsBizSuccess){
                            var table = $('#actiontable').DataTable();
                            table.ajax.reload();
                            if(menu.actionindex >= 0){
                                layer.msg('修改成功！');
                            }else{
                                layer.msg('添加成功！');
                            }
                        }else{
                            if(menu.actionindex >= 0){
                                layer.msg('修改失败！');
                            }else{
                                layer.msg('添加失败！');
                            }
                        }
                    }
                });
            });
            //编辑菜单
            $(document).on('click','.EditAction',function(){
                $("#addAction .modal-title").html("编辑模块");
                var index = $(this).closest("tr").index();
                menu.actionindex = index;
                var data = menu.actionList[index];
                $("#addAction .ActionName").val(data.ActionName);
                $("#addAction .ActionCode").val(data.ActionCode);
                $("#addAction .AppRange").val(data.AppRange);
                console.log(data);
                menu.showmessage($("#addAction .error"),false);
                $("#addAction").modal("show");
            });

            //input输入
            $(document).on('input','.table-sort-input',function(){
                var string = $(this).val();
                var str = string.replace(/\D|/g,'');
                $(this).val(str);
            });
            $(document).on('click','.SaveRelationShip',function(){
                var actionList =[];
                //var actiontable = $("#actionlist").dataTable();
                var actiontable = menu.action;
                var tableList = actiontable.fnGetNodes(); //fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                var ActionList = [];
                $(tableList).each(function(k,v){
                        var obj = $(v).find("input[type='checkbox']");
                    if(obj.is(':checked')){
                        var data = {
                            ActionId : obj.data("actionid"),
                            Sort : $(v).find("td").eq(3).find("input").val(),
                        }
                        ActionList.push(data);
                    }
                });
                var JsonData = {
                    ControllerId:menu.ControllerId,
                    ActionList:ActionList
                }
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: '/jurisdiction/action-map-save',
                    data: JsonData,
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            layer.msg('保存操作成功！');
                        }else{
                            layer.msg('操作操作失败！');
                        }
                    }
                });

            });
        },
        showmessage:function(_obj,isshow,str){
            if(isshow){
                _obj.html(str).show();
            }else{
                _obj.html('').hide();
            }
        },
        //重置表单
        resetFrom:  function (_obj){
            _obj.find("form").each(function (index) {
                _obj.find("form")[index].reset();
            });
            menu.showmessage(_obj.find(".error"),false);
        }
    }
    menu.init();
});
